﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Web.Security;
using Microsoft.Win32;

namespace WebBrowser
{
    public class regedit
    {
        public static long visitCount = 0;
        public static bool REGISTERED = false;
        public static string machCode = "";
        public static string AuthCode = "";

        #region 注册
        public static bool judge_registered()
        {
            bool register = false;
            machCode = get_harddisk_serial();
            AuthCode = DCB001(MD5(machCode));
            if ((read_regidit() == AuthCode) && (AuthCode != ""))
            {
                register = true;
            }
            return register;
        }
        public static string get_harddisk_serial()
        {
            string HDid = "";
            ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid = (string)mo.Properties["Model"].Value;
                //machineCode = HDid;
                //MessageBox.Show(HDid);
                return HDid;

            }
            return "获取机器码出错!";
        }
        public static string read_regidit()
        {
            string regInfo = "";
            try
            {
                RegistryKey myreg = Registry.LocalMachine.OpenSubKey("software\\Doucube");
                regInfo = myreg.GetValue("QZoneBrowser").ToString();
                myreg.Close();
            }
            catch (Exception)
            {
                regInfo = "";
                //throw;
            }
            return regInfo;
        }
        public static void write_regidit(string value)
        {
            RegistryKey key = Registry.LocalMachine;
            RegistryKey createDcbKey = key.CreateSubKey("software\\Doucube");
            RegistryKey writeDcbKey = key.OpenSubKey("software\\Doucube", true); //该项必须已存在
            writeDcbKey.SetValue("QZoneBrowser", value, RegistryValueKind.String);
            key.Close();
        }
        public static string SHA1(string source)
        {
            return (source != "") ? (FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1")) : "";

        }
        public static string MD5(string source)
        {
            return (source != "") ? (FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5")) : "";
        }
        public static string DCB001(string source)
        {
            List<string> mList = new List<string>();
            for (int i = 0; i < source.Length; i += 3)
            {
                string x = "";
                x = source.Substring(i, 1);
                if (i == 3 * 1)
                {
                    mList.Add("B");
                }
                else if (i == 3 * 5)
                {
                    mList.Add("5");
                }
                else if (i == 3 * 9)
                {
                    mList.Add("A");
                }
                else
                {
                    mList.Add(x);
                }
            }
            return string.Join("", mList.ToArray());
        }
        #endregion
    }
}
